package com.yuda.campsite.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.yuda.campsite.domain.CampsiteConf;
import com.yuda.campsite.domain.SpartaLog;
import com.yuda.campsite.service.ICampsiteConfService;
import com.yuda.campsite.service.ICampsiteDistributionService;
import com.yuda.campsite.service.impl.CampsiteConfServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yuda.common.log.annotation.Log;
import com.yuda.common.log.enums.BusinessType;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.campsite.domain.CampsiteDistribution;

import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author wyn
 * @date 2021-12-09
 */
@RestController
@RequestMapping("/distribution")
public class CampsiteDistributionController extends BaseController {


    @Autowired
    private ICampsiteDistributionService campsiteDistributionService;

    @Autowired
    private ICampsiteConfService campsiteConfService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "common:distribution:list")
    @GetMapping("/list")
    public TableDataInfo list(CampsiteDistribution campsiteDistribution) {
        startPage();
        List<CampsiteDistribution> list = campsiteDistributionService.selectCampsiteDistributionList(campsiteDistribution);
        list.forEach(x -> x.setVisitUrl("http://ying.testgate.cn/" + x.getUuid()));
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize(hasPermi = "common:distribution:export")
    @Log(title = "导出分销商日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,Integer campsiteId) throws IOException {
//        List<CampsiteDistribution> list = campsiteDistributionService.selectCampsiteDistributionList(campsiteDistribution);
//        ExcelUtil<CampsiteDistribution> util = new ExcelUtil<CampsiteDistribution>(CampsiteDistribution.class);
//        util.exportExcel(response, list, "distribution");
        List<SpartaLog> list = campsiteDistributionService.selectDistributionLogs(campsiteId);
        ExcelUtil<SpartaLog> util = new ExcelUtil<>(SpartaLog.class);
        util.exportExcel(response, list, "log");

    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize(hasPermi = "common:distribution:query")
    @GetMapping(value = "/{uuid}")
    public AjaxResult getInfo(@PathVariable("uuid") String uuid) {
        return AjaxResult.success(campsiteDistributionService.selectCampsiteDistributionById(uuid));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "common:distribution:add")
    @Log(title = "添加分销商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CampsiteDistribution campsiteDistribution) {
        String s = UUID.randomUUID().toString().replaceAll("-","");
        campsiteDistribution.setUuid(s);
        //根据营地的id 查询营地后缀
        CampsiteConf campsiteConf = campsiteConfService.selectCampsiteConfById(campsiteDistribution.getCampId());
        campsiteDistribution.setCampIdentity(campsiteConf.getIdentity());
        return toAjax(campsiteDistributionService.insertCampsiteDistribution(campsiteDistribution));
    }

    /**
     * 修改【请填写  功能名称】
     */
//    @PreAuthorize(hasPermi = "common:distribution:edit")
    @Log(title = "编辑分销商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CampsiteDistribution campsiteDistribution) {
        return toAjax(campsiteDistributionService.updateCampsiteDistribution(campsiteDistribution));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize(hasPermi = "common:distribution:remove")
    @Log(title = "删除分销商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{uuids}")
    public AjaxResult remove(@PathVariable String[] uuids) {
        return toAjax(campsiteDistributionService.deleteCampsiteDistributionByIds(uuids));
    }




}
